// 获取路径query参数
export const getQuery = (query:string,key:string)=>{
    return new URLSearchParams(query).get(key) || ''
}

/**
 * 函数防抖
 * @param {Function} func
 * @param {number} delay
 * @param {boolean} immediate
 * @return {*}
 */

export function debounce(this: any, func: { apply: (arg0: any, arg1: any[]) => void; }, delay: number | undefined, immediate = false) {
    let timer: any,
        context = this;
    return (...args: any) => {
        if (immediate) {
            func.apply(context, args);
            immediate = false;
            return;
        }
        clearTimeout(timer);
        timer = setTimeout(() => {
            func.apply(context, args);
        }, delay);
    };
}

// 定义一个深拷贝函数
export const deepClone = (target: any): any => {
    // 定义一个变量
    let result: any;
    // 如果当前需要深拷贝的是一个对象的话
    if (typeof target === 'object') {
        // 如果是一个数组的话
        if (Array.isArray(target)) {
            result = []; // 将result赋值为一个数组，并且执行遍历
            for (let i in target) {
                // 递归克隆数组中的每一项
                result.push(deepClone(target[i]));
            }
            // 判断如果当前的值是null的话；直接赋值为null
        } else if (target === null) {
            result = null;
            // 判断如果当前的值是一个RegExp对象的话，直接赋值
        } else if (target.constructor === RegExp) {
            result = target;
        } else {
            // 否则是普通对象，直接for in循环，递归赋值对象的所有值
            result = {};
            for (let i in target) {
                result[i] = deepClone(target[i]);
            }
        }
        // 如果不是对象的话，就是基本数据类型，那么直接赋值
    } else {
        result = target;
    }
    // 返回最终结果
    return result;
}

//参数重置
export const resetParams = (origin: any, target: any) => {
    for (const key in target) {
        target[key] = origin[key];
    }
    return target;
}

export const upperCaseString = (str: string) => {
    var result = '';
    for (var i = 0; i < str.length; i++) {
        var temp = str.charAt(i);
        var code = temp.charCodeAt(0);
        if ('a' <= temp && temp <= 'z') {
            temp = String.fromCharCode(code - 32);
        }
        result += temp;
    }
    return result;
}

export const Uint8ArrayToString = (array: string | any[]) => {
    var out, i, len, c;
    var char2, char3;

    out = '';
    len = array.length;
    i = 0;
    while (i < len) {
        c = array[i++];
        switch (c >> 4) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                // 0xxxxxxx
                out += String.fromCharCode(c);
                break;
            case 12:
            case 13:
                // 110x xxxx 10xx xxxx
                char2 = array[i++];
                out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
                break;
            case 14:
                // 1110 xxxx 10xx xxxx 10xx xxxx
                char2 = array[i++];
                char3 = array[i++];
                out += String.fromCharCode(((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0));
                break;
        }
    }

    return out;
}
// 定义一个函数：isValidKey()，然后对需要使用到的地方进行一次判断
export function isValidKey(
    key: string | number | symbol,
    object: object
): key is keyof typeof object {
    return key in object;
}